package io.adrainty.bolee.security.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.adrainty.bolee.security.mapper.ICompanyMapper;
import io.adrainty.bolee.security.models.bo.CompanyBO;
import io.adrainty.bolee.security.models.constants.CompanyConstants;
import io.adrainty.bolee.security.models.entity.CompanyEntity;
import io.adrainty.bolee.security.repository.ICompanyRepository;
import jakarta.annotation.Resource;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Repository;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>CompanyRepositoryImpl</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description ICompanyRepository
 * @since 2025/8/1 13:53:58
 */

@Repository
public class CompanyRepositoryImpl implements ICompanyRepository {

    @Resource
    private ICompanyMapper companyMapper;

    @Resource
    private RedissonClient redissonClient;

    @Override
    public List<CompanyEntity> listValidateCompany() {
        Wrapper<CompanyBO> wrapper = Wrappers.lambdaQuery(CompanyBO.class)
                .in(CompanyBO::getStatus, CompanyConstants.VALIDATED_COMPANY_STATUS)
                .orderByAsc(CompanyBO::getCreatedTime);
        List<CompanyBO> selectList = companyMapper.selectList(wrapper);
        return BeanUtil.copyToList(selectList, CompanyEntity.class);
    }

    @Override
    public void refreshCompanyWebsiteCache(CompanyEntity companyEntity) {
        if (companyEntity == null) return;
        LocalDateTime expireTime = companyEntity.getExpireTime();
        Duration duration = Duration.between(LocalDateTime.now(), expireTime);
        long seconds = duration.toSeconds();
        if (seconds <= 0) return;

        String webSite = companyEntity.getWebSite();
        String appSite = companyEntity.getAppSite();
        if (StringUtils.isNotBlank(webSite)) {
            RBucket<CompanyEntity> webSiteBucket = redissonClient.getBucket(CompanyConstants.COMPANY_WEBSITE_CACHE_KEY + webSite);
            webSiteBucket.set(companyEntity, Duration.ofSeconds(seconds));
        }

        if (StringUtils.isNotBlank(appSite)) {
            RBucket<CompanyEntity> appSiteBucket = redissonClient.getBucket(CompanyConstants.COMPANY_WEBSITE_CACHE_KEY + appSite);
            appSiteBucket.set(companyEntity, Duration.ofSeconds(seconds));
        }
    }

    @Override
    public void removeCompanyWebsiteCache(CompanyEntity companyEntity) {
        if (companyEntity == null) return;

        String webSite = companyEntity.getWebSite();
        String appSite = companyEntity.getAppSite();

        if (StringUtils.isNotBlank(webSite)) {
            RBucket<CompanyEntity> webSiteBucket = redissonClient.getBucket(CompanyConstants.COMPANY_WEBSITE_CACHE_KEY + webSite);
            webSiteBucket.delete();
        }

        if (StringUtils.isNotBlank(appSite)) {
            RBucket<CompanyEntity> appSiteBucket = redissonClient.getBucket(CompanyConstants.COMPANY_WEBSITE_CACHE_KEY + appSite);
            appSiteBucket.delete();
        }
    }

    @Override
    public CompanyEntity findCompanyEntity(String companyNo) {
        if (StringUtils.isBlank(companyNo)) return null;
        Wrapper<CompanyBO> wrapper = Wrappers.lambdaQuery(CompanyBO.class)
                .in(CompanyBO::getCompanyNo, companyNo);
        CompanyBO companyBO = companyMapper.selectOne(wrapper);
        if (companyBO == null) return null;
        return BeanUtil.copyProperties(companyBO, CompanyEntity.class);
    }

    @Override
    public CompanyEntity createCompany(CompanyEntity companyEntity) {
        if (companyEntity == null) return null;
        CompanyBO companyBO = BeanUtil.copyProperties(companyEntity, CompanyBO.class);
        if (companyMapper.insert(companyBO) <= 0) return null;
        return BeanUtil.copyProperties(companyBO, CompanyEntity.class);
    }
}
